package com.icez.verticalhorizontalrolinglibrary

import android.content.Context
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @sample 上下滚动，左右滚动的
 * @author icez
 */
class RollingLayoutManager @JvmOverloads constructor(
    val context: Context?,
    orientation: Int = LinearLayoutManager.VERTICAL,
    reverseLayout: Boolean = false
) : LinearLayoutManager(context, orientation, reverseLayout),
    RecyclerView.OnChildAttachStateChangeListener {

    private var mPagerSnapHelper: PagerSnapHelper? = null
    private var mCurrentPosition = 0
    private var mLastPosition = 0

    private var mOnCurrentPositionListener: OnCurrentPositionListener? = null
    var isCanScrollVertically: Boolean = true

    private var isA = false


    init {
        this.mPagerSnapHelper = PagerSnapHelper()
    }

    override fun canScrollVertically(): Boolean {
        return isCanScrollVertically
    }

    override fun onAttachedToWindow(view: RecyclerView?) {
        super.onAttachedToWindow(view)
        view?.addOnChildAttachStateChangeListener(this)
        this.mPagerSnapHelper?.attachToRecyclerView(view)
    }

    override fun onChildViewAttachedToWindow(view: View) {
        val currentPosition = getPosition(view)
        when (currentPosition) {
            0 -> {
                if(!isA){
                    isA = true
                    mCurrentPosition = currentPosition
                    mOnCurrentPositionListener?.currentPositionListener(
                        view,
                        mCurrentPosition
                    )
                }


            }

        }
    }

    override fun onChildViewDetachedFromWindow(view: View) {


        val currentPosition = getPosition(view)
        mOnCurrentPositionListener?.currentDetachedPositionListener(view, currentPosition)



    }

    override fun onScrollStateChanged(state: Int) {
        when (state) {
            RecyclerView.SCROLL_STATE_IDLE -> {
                val currentView = this.mPagerSnapHelper?.findSnapView(this)
                val currentPosition = currentView?.let { getPosition(it) }?:0

                if(mCurrentPosition!=currentPosition){
                    mCurrentPosition = currentPosition
                    mOnCurrentPositionListener?.currentPositionListener(currentView, mCurrentPosition)
                }
            }
        }
        super.onScrollStateChanged(state)
    }

    public fun setOnCurrentPositionListener(mOnCurrentPositionListener: OnCurrentPositionListener) {
        this.mOnCurrentPositionListener = mOnCurrentPositionListener
    }

    interface OnCurrentPositionListener {
        fun currentPositionListener(view: View?, currentPosition: Int?)
        fun currentDetachedPositionListener(view: View?, currentPosition: Int?)
    }

}